package com.bms.admin.auth;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.bms.admin.base.menu.entity.Menu;
import com.bms.admin.base.menu.service.IMenuService;
import com.bms.admin.base.user.service.IUserService;
import com.bms.auth.SubjectUtil;
import com.bms.auth.service.IUserRealm;
import com.feilong.core.Validator;
import com.feilong.core.util.CollectionsUtil;

public class UserRealm extends IUserRealm{
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IMenuService menuService;

	@Override
	public Set<String> getUserRoles(String userId) {
		Set<String> roles = new HashSet<>();
		roles.add(userService.selectById(userId).getRoleId().toString());
		return roles;
	}

	@Override
	public Set<String> getUserPermissions(String userId) {
		Set<String> permissionValues = new HashSet<>();
		List<String> userRoles = SubjectUtil.getInstance().getUserRoles(userId);
		if(Validator.isNotNullOrEmpty(userRoles)){
			List<Menu> permissions = menuService.queryListByRoleId(Long.parseLong(userRoles.get(0)));
			if(Validator.isNotNullOrEmpty(permissions)){
				permissionValues.addAll(CollectionsUtil.getPropertyValueList(permissions, "perms"));
			}
		}
		return permissionValues;
	}

}
